package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.mapper.GScoreMapper;
import com.team11.server.mapper.PInformationMapper;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.mapper.TInformationMapper;
import com.team11.server.pojo.*;
import com.team11.server.pojo.dto.MScoreDetails;
import com.team11.server.pojo.dto.RespBean;
import com.team11.server.pojo.dto.RespPageBean;
import com.team11.server.service.IGScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pjc
 * @since 2021-07-08
 */
@Service
public class GScoreServiceImpl extends ServiceImpl<GScoreMapper, GScore> implements IGScoreService {

    @Resource
    private GScoreMapper gScoreMapper;
    @Resource
    private TInformationMapper tInformationMapper;
    @Resource
    private SInformationMapper sInformationMapper;
    @Resource
    private PInformationMapper pInformationMapper;


    /**
     * 获取小组评分页面
     *
     * @param current s
     * @param size    s
     * @param pName   s
     * @param kcName  s
     * @return s
     */
    @Override
    public RespPageBean getGroupScoreList(Integer current, Integer size, String pName, String kcName) {
        IPage<Map<String, Object>> page = new Page<>(current, size);
        IPage<Map<String, Object>> groupScoreList = gScoreMapper.getGroupScoreList(page, kcName, pName);
        return new RespPageBean(groupScoreList.getTotal(), groupScoreList.getRecords());
    }

    /**
     * 获取小组综合评分详情
     *
     * @param current s
     * @param size    s
     * @param gId     s
     * @return s
     */
    @Override
    public RespPageBean getGScoreDetails(Integer current, Integer size, Integer gId, Integer type) {
        Map<String, Object> map = gScoreMapper.getGroupDetails(gId);
        //获取小组长
        try {
            Integer pId = gScoreMapper.selectOne(new QueryWrapper<GScore>().eq("g_id", gId)).getPId();
            String sid = pInformationMapper.selectOne(new QueryWrapper<PInformation>().eq("p_id", pId)).getPSetLeader();
            String sName = null;
            if (sid != null) {
                sName = sInformationMapper.selectOne(new QueryWrapper<SInformation>().eq("sid", sid)).getSName();
            }
            map.put("leader", sName);
//            list.add(sName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        IPage<MScoreDetails> page = new Page<>(current, size);
        IPage<MScoreDetails> gScoreDetails = gScoreMapper.getGScoreDetails(page, gId, type);
        List<Object> list = new ArrayList<>();
        list.add(gScoreDetails.getRecords());
        list.add(map);
        return new RespPageBean(gScoreDetails.getTotal(), list);
    }

    @Override
    public RespBean GroupComprehensiveScore(Integer gId, String tJobNumber, Integer tId, String gScore, String gContent) {
        String number = tInformationMapper.selectOne(new QueryWrapper<TInformation>().eq("t_id", tId)).getTJobNumber();

        if (!tJobNumber.equals(number)) {
            return RespBean.error("权限不足，您不能对该小组进行综合评分!");
        }

        GScore gScore1 = new GScore();
        gScore1.setGScore(gScore);
        gScore1.setGContent(gContent);
        gScore1.setGId(gId);
        if (gScoreMapper.updateById(gScore1) == 1) {
            return RespBean.success("操作成功!");
        }
        return RespBean.error("操作失败！");
    }
}
